
//  BottomViewController.m
//  OurMenu
//
//  Created by 尹坤 on 15/10/10.
//  Copyright © 2015年 尹坤. All rights reserved.
//

#import "BottomViewController.h"
#import "WaterLayout.h"
#import "ShopMode.h"
#import "ShopCell.h"
#import "MJExtension.h"
#import "IANshowLoading.h"
#import "DetailsViewController.h"
static NSString *const Id = @"shop";

@interface BottomViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,WaterLayoutDelegate>
{
    DownLoadDataHelper *down;
    NSString  *NowStr;
    NSInteger number;
    //NSString *str;
    NSString *urlStr;
    UIView *loadingView;
    UIImageView *collectionimage;
}

@property(nonatomic,weak)UICollectionView *collectionView;
@property(nonatomic,strong)NSMutableArray *shopArr;



@end

@implementation BottomViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = self.str;
    NSArray *shopArray = [ShopMode objectArrayWithFilename:@"1.plist"];
    [self.shopArr addObjectsFromArray:shopArray];
    [self _initCollectionView];
    down = [[DownLoadDataHelper alloc] init];
    down.sendDataDelegete = self;
    
    //self.str = @"早餐";
    urlStr = [NSString stringWithFormat:@"http://apis.juhe.cn/cook/query?key=2392dbacea9127e5beb16b0758c3fcaa&menu=%@&rn=10&pn=1",self.str];
    // ios 9转码
    NowStr = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    [self.collectionView addFooterWithTarget:self action:@selector(dragUp)];
    number = 1;
    [down downLoadDataWithUrlString:NowStr];
    self.dataArray = [NSMutableArray array];
    
}



- (void)_initCollectionView {
    
    WaterLayout *water = [[WaterLayout alloc] init];
    
    water.delegate = self;
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64 - 44) collectionViewLayout:water];
    
    self.collectionView = collectionView;
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    self.collectionView.backgroundColor = [UIColor colorWithRed:236/255.0 green:238/255.0 blue:234/255.0 alpha:1];
    //self.collectionView.backgroundColor = [UIColor grayColor];
    [self.collectionView registerNib:[UINib nibWithNibName:@"ShopCell" bundle:nil] forCellWithReuseIdentifier:Id];
    //添加记载视图
    collectionimage = [[UIImageView alloc] initWithFrame:self.view.frame];
    [self.view addSubview:collectionimage];
    collectionimage.image = [UIImage imageNamed:@"7197a08e13a4f608a369cece49190ca8"];
    
    [self.view addSubview:self.collectionView];
    
    [IANshowLoading showGrayLoadingForView:collectionimage allowUserInteraction:YES];
   
}

- (void)sendDataBack:(NSData *)data isRequest:(BOOL)isRequest
{
    if (isRequest) {
        
        NSDictionary *allDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSDictionary *towDic = [allDic objectForKey:@"result"];
        NSArray *allArray = [towDic objectForKey:@"data"];
        for (NSDictionary *dic in allArray) {
            DishesMadels *dishes = [[DishesMadels alloc] init];
            [dishes setValuesForKeysWithDictionary:dic];
            [self.dataArray addObject:dishes];
        }
        [self.collectionView reloadData];
        [IANshowLoading hideLoadingForView:collectionimage];
    }else{
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"网络连接失败" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alert animated:YES completion:nil];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *  action) {
            [self dismissViewControllerAnimated:YES completion:nil];
            [self.navigationController popToRootViewControllerAnimated:YES];
        }];
        [alert addAction:action ];
    }
    
}

- (void)dragUp
{
    number = (number++)*10 + 1;
    //NSLog(@"%ld",number);
    NSString *string = [NSString stringWithFormat:@"%ld",number];
    //self.str = @"早餐";
    urlStr = [NSString stringWithFormat:@"http://apis.juhe.cn/cook/query?key=2392dbacea9127e5beb16b0758c3fcaa&menu=%@&rn=10&pn=%@",self.str,string];
    // ios 9转码
    NowStr = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    [down downLoadDataWithUrlString:NowStr];
    
    [self.collectionView footerEndRefreshing];
    
}


-(NSMutableArray *)shopArr{
    if (_shopArr == nil) {
        
        self.shopArr = [[NSMutableArray alloc] init];
    }
    return _shopArr;
}


#pragma mark  自定义layout的代理方法
-(CGFloat)waterLayout:(WaterLayout *)waterLayout heightForWidth:(CGFloat)width atIndexPath:(NSIndexPath *)indexPath{
    
    ShopMode *shopMode = self.shopArr[indexPath.item];
    CGFloat height = shopMode.h / shopMode.w * width;
    return height;
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    //return self.shopArr.count;
    return self.dataArray.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    ShopCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:Id forIndexPath:indexPath];
    
    //DishesMadels *dish = [[DishesMadels alloc] init];
    DishesMadels *dish = self.dataArray[indexPath.row];
    cell.dish = dish;
    
    return cell;
    
    
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    DetailsViewController *detail = [[DetailsViewController alloc] init];
    detail.dish = self.dataArray[indexPath.row];
    [self.navigationController pushViewController:detail animated:NO];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end